#include <NewSoftSerial.h>
#include <EEPROM.h> //Needed to access the eeprom read write functions

/*********************************************
 * DAC MCP4922 dual DAC with SPI input
 * 
 * Tone ping output PIN = 6;   to delay pedal input
 * Start calibration switch PIN = 4;  START CALIBRATION switch
 * listen input for ping PIN = 3;   input from 7556 timer pulse detector cct / delay pedal wet output
 * default "SHORT" setting on delay = 5 (ground pin for "LONG" setting, open for "SHORT")
 * relay_out_Pin = 7; set high to "insert" inputs / output for calibration 
 * BLINKIE LED for TAP TEMPO is PIN = 18 (analog 4)
 * TAP TEMPO slow Switch PIN = 8 connect this to TIP of jack
 * TAP TEMPO fast Switch PIN = 9 connect to RING of stereo jack
 * Subdivision Switch for TAP TEMPO analog PIN = A0
 * LCD Tx on analog PIN = A1
 * LCD Rx (unused) on analog PIN = A2
 * BYPASS switch PIN = 2 (HIGH is BYPASS)
 * 
 * The calibration routine yields three tables after checking for signal integrity and linearity of the readings
 * 1) The time array with 17 samples of DAC voltage output vs the two (long and short) delay mode timings
 * 2) The optimum max/min positions in the table so we start interpolating where in the linear part of the readings
 * 3) The optimum max/min values to deterimine if the requested time delay is in range for the delay mode
 * The calibration routine will populate the SHORT and LONG data regardless of switch position - by initially checking ping length
 * not forcing user to switch the modes  
 * delay "too short", "too long" and "in range" LED's added to the LCD serial board.
 
 
 
 to do list
 
 1) DONE add code to interpolate to yied volt for a given time value - find a pair of points to interpolate between with stright line interp
 2) DONE add code for tap tempo from pedal project  - with selectable subdivisons 
 3) DONE midi clock input to adjust tap tempo - with selectable subdivisions, simply added midi to emulate a "tap"
 added special MIDI display BPM that refreshes screen only occasionally, and give a message the time unput is under midi control. 
 Implimented new software serial, the original software serial occasionally writes junk to the LCD. This one seems to be much better behaved.
 4) fine tune signal electronics front end and add display for messages: May 5 - adjusted voltage ladder to inputs of comparators, solved the problem of low output from the 555 timers, 
 input is adequate level with some room to spare, but still may want to put an amplifier on the input for a little more gain.
 
5) fixed a funky tap tempo function, the two debounce channels from ext footswitch were not exactly the same decay. Now waits for a steady state. 
6) still issues with led indicator lights turning off and on properly with a bypass swicthed in and out. the last state may need to be saved and recalled on bypass. 
 
 *********************************************/

#define DATAOUT 11//MOSI           standard SPI stuff
#define DATAIN  12//MISO 
#define SPICLOCK  13//sck
#define SLAVESELECT 10//ss

const int ping_out_Pin = 6;             //pin assignments
const int ping_listen_Pin = 3;
const int start_calibration_Pin = 4;
const int SHORT_select_Pin = 5;         //if this is high (default) the we are using the "fast" setting on the delay
const int relay_out_Pin = 7;    //set high for calibration
const int POT_A = 0;            //analog in for tap subdivide selector switch
const int BYPASS = 2;

//LCD display stuff 
#define rxPin 16 // pin 16 is analog pin 2, on a servo cable
#define txPin 15 // pin 15 is analog pin 1, on a servo cable
NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin);  //more stable operation

// TAP TEMPO function variables
const int A_SW = 8; //slow switch   connect to TIP of stereo cable (so this can work on a mono cable)
const int B_SW = 9; //fast switch
const int tapLED = 18;   //LED flash for tap tempo
int tapDelta = 0;
int   BPM = 0;
unsigned long millis_tap_tempo = 2083; //tap tempo in milliseconds corresponds to initial 500 msec / 24
unsigned long disp_millis_tap_tempo = 0; //tap tempo in milliseconds corresponds to initial 500 msec / 24
int Subdivision = 24; //subdivision variable  for tap temp sub-beats - default is a quarter note in midi ticks

byte y = 0; //mode variable: 0 => SHORT 1 => LONG mode

volatile int long time_stamp = 0;    //initial stamp of arriving pulses
volatile int long delay_time = 0;     // calculated delay value
volatile int long seg_start = 0;      //watchdog timer for lost signals
volatile boolean listen = false;      // tells the loop to resume after measuring pulse delay
volatile boolean calibration = false;           // ISR check for valid calibration state before measuring delay
volatile byte ping_count = 0;   //quality check for ISR 
boolean SHORT_setting = HIGH;   //keeps track of mode mooger delay is in - fast or slow switch

int m_sec = 0;    //the calculated milisecond delay period from tempo calcs 
int msec_old = 0; //test if anything changed

// the two bytes that go to the DAC over SPI
byte dacSPI0;
byte dacSPI1;

//midi input stuff 
byte midi_start = 0xfa;  //midi sysex 
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;








